package de.bk.timecalc.core;

/**
 * Transfers object for time. TimeDTOs are immutable.
 * 
 * Test: {@link TimeDTOTest}
 * 
 * @author Bastian Krol
 * @version $Revision:$ $Date:$ $Author:$
 */
public class TimeDTO
{
  private final int minute;
  private final int second;

  /**
   * Creates a TimeDTO.
   * 
   * @param minute
   *          the minute to start the calculation from
   * @param second
   *          the second to start the calculation from
   */
  public TimeDTO(int minute, int second)
  {
    super();
    if (minute < 0)
    {
      throw new IllegalArgumentException("Negative minute is not allowed.");
    }
    if (minute > 59)
    {
      throw new IllegalArgumentException("Minute > 59 is not allowed.");
    }
    if (second < 0)
    {
      throw new IllegalArgumentException("Negative second is not allowed.");
    }
    if (second > 59)
    {
      throw new IllegalArgumentException("Second > 59 is not allowed.");
    }
    this.minute = minute;
    this.second = second;
  }

  /**
   * Liefert minute.
   * 
   * @return liefert minute.
   */
  public final int getMinute()
  {
    return this.minute;
  }

  /**
   * Liefert second.
   * 
   * @return liefert second.
   */
  public final int getSecond()
  {
    return this.second;
  }

  /**
   * {@inheritDoc}
   * 
   * @see java.lang.Object#hashCode()
   */
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + this.minute;
    result = prime * result + this.second;
    return result;
  }

  /**
   * {@inheritDoc}
   * 
   * @see java.lang.Object#equals(java.lang.Object)
   */
  public boolean equals(Object obj)
  {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    TimeDTO other = (TimeDTO) obj;
    if (this.minute != other.minute)
      return false;
    if (this.second != other.second)
      return false;
    return true;
  }

  /**
   * {@inheritDoc}
   * 
   * @see java.lang.Object#toString()
   */
  public String toString()
  {
    StringBuffer stringBuffer = new StringBuffer();
    if (this.minute < 10)
    {
      stringBuffer.append("0");
    }
    stringBuffer.append(this.minute);
    stringBuffer.append(":");
    if (this.second < 10)
    {
      stringBuffer.append("0");
    }
    stringBuffer.append(this.second);
    return stringBuffer.toString();
  }

  /**
   * Returns the difference in seconds when subtracting <code>timeDTO</code>
   * from this TimeDTO.
   * 
   * @param timeDTO
   *          the TimeDTO to subtract
   * @return the difference in seconds when subtracting <code>timeDTO</code>
   *         from this TimeDTO
   */
  public int subtract(TimeDTO timeDTO)
  {
    return ((this.minute - timeDTO.minute) * 60)
        + (this.second - timeDTO.second);
  }
}
